package team.seekers.catering.controller.api;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mysql.cj.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import team.seekers.catering.common.result.R;
import team.seekers.catering.common.result.ResultCode;
import team.seekers.catering.entity.dataobject.Food;
import team.seekers.catering.entity.params.add.FoodAddPA;
import team.seekers.catering.entity.params.add.FoodUpdatePA;
import team.seekers.catering.entity.params.query.FoodQuery;
import team.seekers.catering.entity.vo.info.FoodInfoVO;
import team.seekers.catering.service.FoodService;
import team.seekers.catering.service.IngredientService;

import java.util.Map;

/**
 * <p>
 * 食物表 前端控制器
 * </p>
 *
 * @author hbc
 * @since 2022-02-04
 */
@RestController
@RequestMapping("/food")
public class FoodController {

    @Autowired
    private FoodService foodService;
    @Autowired
    private IngredientService ingredientService;

    /**
     * 查询食谱
     */
    @PostMapping("/adminPageOrder/{current}/{limit}")
    public R adminPageOrder(@PathVariable("current") long current,
                            @PathVariable("limit") long limit,
                            @RequestBody FoodQuery query){
        QueryWrapper<Food> wrapper = new QueryWrapper<>();
        if(!StringUtils.isNullOrEmpty(query.getNameKey())){
            wrapper.like("name",query.getNameKey());
        }
        Map<String, Object> map = foodService.adminPageOrder(current,limit,wrapper);
        if(map == null){
            return R.fail(ResultCode.GET_FAIL);
        }
        return R.success(map);
    }


    /**
     * 新增食谱
     */
    @PostMapping("/add")
    public R add(@RequestBody FoodAddPA foodAddPA){
        ingredientService.setParamFromList(foodAddPA);
        String id = foodService.add(foodAddPA);
        return R.success().data("id",id);
    }

    /**
     * 修改食谱
     */
    @PostMapping("/update")
    public R update(@RequestBody FoodUpdatePA foodUpdatePA){
        ingredientService.setParamFromList(foodUpdatePA);
        String id = foodService.update(foodUpdatePA);
        return R.success().data("id",id);
    }

    /**
     * 查询食谱
     */
    @GetMapping("/query/{foodId}")
    public R query(@PathVariable("foodId")String id){
        FoodInfoVO foodInfoVO = foodService.get(id);
        return R.success().data("info",foodInfoVO);
    }

    /**
     * 删除食谱
     */
    @GetMapping("/delete/{foodId}")
    public R delete(@PathVariable("foodId")String id){
        foodService.removeById(id);
        return R.success();
    }

    /**
     * 查询今日推荐食谱
     */
    @GetMapping("/todayPush")
    public R todayPush(){
        FoodInfoVO foodInfoVO = foodService.todayPush();
        return R.success().data("info",foodInfoVO);
    }


}

